/*
 * TOP SECRET Copyright 2006-2015 Transsion.com All right reserved. This software is the confidential and proprietary
 * information of Transsion.com ("Confidential Information"). You shall not disclose such Confidential Information and
 * shall use it only in accordance with the terms of the license agreement you entered into with Transsion.com.
 */
package com.yunji.framework_template.biz.crawler.tz;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.yunji.framework_template.biz.crawler.ContentImageHandler;
import com.yunji.framework_template.biz.crawler.NewsCrawler;
import com.yunji.framework_template.biz.crawler.SourceType;
import com.yunji.framework_template.common.enumeration.CountryCode;
import com.yunji.framework_template.common.enumeration.NewsType;

/**
 * ClassName:NationNewsCrawler <br/>
 * Date: 2018年12月18日 下午3:31:24 <br/>
 * 
 * @author fenglibin1982@163.com
 * @Blog http://blog.csdn.net/fenglibing
 * @version
 * @see
 */
@Service
public class MwananchiNewsCrawler extends NewsCrawler {

    @Resource
    private MwananchiNewsImageHandler mwananchiNewsImageHandler;

    @Override
    public boolean isOkUrl(String url) {
        // 通过判断url的path中是否包括年月来判断其是否为新闻详情页
        URI uri;
        try {
            if (url == null || url.trim().length() == 0) {
                return false;
            }
            if (url.startsWith("mailto") || url.toLowerCase().startsWith("javascript")) {
                return false;
            }
            uri = new URI(url);
            String host = uri.getHost();
            if (host.indexOf("mwananchi.co.tz") < 0) {
                return false;
            }
            String path = uri.getPath();
            String[] pathArr = path.split("/");
            if (pathArr.length == 4 && pathArr[1].split("-").length > 2 && url.indexOf("index.html") > 0) {
                return true;
            }else if (pathArr.length == 5 && pathArr[2].split("-").length > 2 && url.indexOf("index.html") > 0) {
                return true;
            } else if (pathArr.length == 6 && pathArr[3].split("-").length > 2 && url.indexOf("index.html") > 0) {
                return true;
            }
        } catch (URISyntaxException e) {
        }
        return false;
    }

    @Override
    public ContentImageHandler getContentImageHandler() {
        return mwananchiNewsImageHandler;
    }

    @Override
    public List<SourceType> getSourceTypeList() {
        List<SourceType> sourceTypeList = new ArrayList<SourceType>();
        sourceTypeList.add(SourceType.builder().url("http://www.mwananchi.co.tz/1596774-1596774-k7tscqz/index.html").newsType(NewsType.HOT).build());
        sourceTypeList.add(SourceType.builder().url("http://www.mwananchi.co.tz/habari/1597578-1597578-hoj2ic/index.html").newsType(NewsType.EDUCATION).build());
        sourceTypeList.add(SourceType.builder().url("http://www.mwananchi.co.tz/Michezo/1597534-1597534-9qb8as/index.html").newsType(NewsType.VIDEO).build());
        sourceTypeList.add(SourceType.builder().url("http://www.mwananchi.co.tz/1597562-1597562-15b7u3tz/index.html").newsType(NewsType.IMAGE).build());
        sourceTypeList.add(SourceType.builder().url("http://www.mwananchi.co.tz/photo/1597602-1597602-13e864pz/index.html").newsType(NewsType.SPORT).build());
        sourceTypeList.add(SourceType.builder().url("http://www.mwananchi.co.tz/Video/2722950-2722950-enxcnrz/index.html").newsType(NewsType.LIFESTYLE).build());
        sourceTypeList.add(SourceType.builder().url("http://www.mwananchi.co.tz/3794314-3794314-1348t4lz/index.html").newsType(NewsType.FUN).build());
        sourceTypeList.add(SourceType.builder().url("http://www.mwananchi.co.tz/habari/biashara/1597570-1597570-nx2th4z/index.html").newsType(NewsType.ASKANDANSWER).build());
        sourceTypeList.add(SourceType.builder().url("http://www.mwananchi.co.tz/habari/Makala/1597592-1597592-sv14fbz/index.html").newsType(NewsType.HEALTH).build());
        sourceTypeList.add(SourceType.builder().url("http://www.mwananchi.co.tz/habari/Kitaifa/Bandari-Dar-yavuka-malengo-mapato--shehena-/1597296-4903404-106euvtz/index.html").newsType(NewsType.MILITARY).build());
        sourceTypeList.add(SourceType.builder().url("http://www.mwananchi.co.tz/habari/Kitaifa/Basata-yafafanua-makosa-matano-yaliyowapeleka/1597296-4903300-e1wa65/index.html").newsType(NewsType.FINANCE).build());
        sourceTypeList.add(SourceType.builder().url("http://www.mwananchi.co.tz/habari/Kitaifa/Ole-wenu-wapinzani--Serikali-yaonya/1597296-4903216-c0egfwz/index.html").newsType(NewsType.BUSINESS).build());
        sourceTypeList.add(SourceType.builder().url("http://www.mwananchi.co.tz/Michezo/1597534-1597534-9qb8as/index.html").newsType(NewsType.ENTERTAINMENT).build());
        sourceTypeList.add(SourceType.builder().url("http://www.mwananchi.co.tz/Michezo/burudani/1597574-1597574-nyhr7pz/index.html").newsType(NewsType.BLOG).build());
        sourceTypeList.add(SourceType.builder().url("http://www.mwananchi.co.tz/Michezo/-Athumani-Juma-Chama-afariki-dunia/1597534-4903314-140qp8kz/index.html").newsType(NewsType.WOMAN).build());
        sourceTypeList.add(SourceType.builder().url("http://www.mwananchi.co.tz/Michezo/-Serikali-yanasa-wanane-waleta-vurugu-Yanga/1597534-4903264-wc9pqmz/index.html").newsType(NewsType.FOOD).build());
        sourceTypeList.add(SourceType.builder().url("http://www.mwananchi.co.tz/Michezo/-Simba-nayo-haitanii/1597534-4903274-gfvt1v/index.html").newsType(NewsType.HOT).build());
        sourceTypeList.add(SourceType.builder().url("http://www.mwananchi.co.tz/Michezo/-CAF--yaichonganisha--Serengeti-Boys/1597534-4903282-8qsxubz/index.html").newsType(NewsType.VIDEO).build());
        return sourceTypeList;
    }

    @Override
    public Set<String> getCountryCodeSet() {
        Set<String> countryCodeSet = new HashSet<String>();
        countryCodeSet.add(CountryCode.TZ.name());
        return countryCodeSet;
    }
}
